package controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import BizLogic.StaffManager;

import dao.StaffDAO;
import dao.mysql.StaffDAOImpl;

import model.Staff;

/**
 * Servlet implementation class SearchStaffServlet
 */
@SuppressWarnings("unused")
@WebServlet("/SearchStaffServlet")
public class SearchStaffServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchStaffServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doprocess(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doprocess(request,response);
	}

	private void doprocess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session=request.getSession(false);
		if(session!=null){
		
		ArrayList<Staff> staffList=new ArrayList<Staff>();
		StaffManager staffManager=new StaffManager();
		String search=request.getParameter("searchByName").toLowerCase();
		search="%"+search+"%";
		if(search==null){
			System.out.println("kk ");
		}
		String trim=search.trim();
		
		staffList=staffManager.searchStaff(trim);
		request.setAttribute("searchedStaff", staffList);
		RequestDispatcher rd=request.getRequestDispatcher("ModifyStaff.jsp");
		rd.forward(request, response);
			

		
		}
		else{
			response.sendRedirect("Login.jsp");
		}
	}

}
